package tk.uodrad.apex.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import tk.uodrad.apex.project.pojo.dto.ActivityAddNewDTO;
import tk.uodrad.apex.project.pojo.dto.ActivityUpdateDTO;
import tk.uodrad.apex.project.pojo.vo.ActivityDashboardVO;
import tk.uodrad.apex.project.pojo.vo.ActivityListVO;
import tk.uodrad.apex.project.service.ActivityManagementService;
import tk.uodrad.apex.project.web.JsonResult;

import java.util.Arrays;
import java.util.List;

/**
 * 说明：
 */
@Slf4j
@RestController
@RequestMapping("/activities")
@Transactional
public class ActivityController {
    @Autowired
    private ActivityManagementService activityService;

    /**
     * 添加成员
     * @param activityAddNewDTO 添加成员DTO
     * */
    @PostMapping("/add")
    public JsonResult<String> add(@RequestBody ActivityAddNewDTO activityAddNewDTO) {
        activityService.save(activityAddNewDTO);
        return JsonResult.ok("添加成功");
    }

    /**
     * 根据ID数组删除成员
     * @param ids ID数组
     * */
    @GetMapping("/delete")
    public JsonResult<String> deleteById(Long[] ids) {
        activityService.removeByIds(Arrays.asList(ids));
        return JsonResult.ok("删除成功！");
    }

    /**
     * 根据ID修改成员信息
     * @param activityUpdateDTO 修改成员DTO
     * */
    @PostMapping("/update")
    public JsonResult<String> update(@RequestBody ActivityUpdateDTO activityUpdateDTO) {
        activityService.updateById(activityUpdateDTO);
        return JsonResult.ok("修改成功！");
    }

    /**
     * 分页查询成员列表
     * @param pageNum 第几页
     * @param pageSize 每页多少条数据
     * @param communityCode 小区编号
     * @return IPage<RepairListVO> RepairListVO分页对象
     * */
    @GetMapping("/list")
    public JsonResult<IPage<ActivityListVO>> list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                  @RequestParam(value = "communityCode", required = false) String communityCode) {
        log.info("后端接收到查列表请求, pageNum:{}, pageSize:{}, communityCode:{}", pageNum, pageSize, communityCode);
        IPage<ActivityListVO> list = activityService.list(pageNum, pageSize, communityCode);
        log.info("service调用完毕, list:{}", list);
        return JsonResult.ok(list);
    }

    @GetMapping("/listDashboard")
    public JsonResult<List<ActivityDashboardVO>> listDashboard() {
        List<ActivityDashboardVO> activityDashboardVOS = activityService.listDashboard();
        return JsonResult.ok(activityDashboardVOS);
    }

    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【启用活动】的请求，参数：{}", id);
        activityService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【禁用活动】的请求，参数：{}", id);
        activityService.setDisable(id);
        return JsonResult.ok();
    }

}
